﻿using System;
using System.Collections.Generic;
using System.Text;
using com.paypal.sdk.profiles;
using System.Configuration;
using com.paypal.soap.api;
using com.paypal.sdk.services;

namespace ebay.ebayApi
{
    public class PayPalAPI
    {
        IAPIProfile profile = null;
        public PayPalAPI(string ApiUserName, string ApiPassWord, string ApiSignature)
        {
            profile = ProfileFactory.createSignatureAPIProfile();
            profile.APIUsername = ApiUserName;
            profile.APIPassword = ApiPassWord;
            profile.APISignature = ApiSignature;
            profile.Environment = "live";
        }

        public GetTransactionDetailsResponseType GetTransactionDetailsInfo(string transactionId)
        {
            CallerServices caller = new CallerServices();
            caller.APIProfile = profile;

            GetTransactionDetailsRequestType concreteRequest = new GetTransactionDetailsRequestType();
            concreteRequest.Version = "51.0";
            concreteRequest.DetailLevel = new DetailLevelCodeType[] { DetailLevelCodeType.ReturnAll };


            concreteRequest.TransactionID = transactionId;
            try
            {
                return (GetTransactionDetailsResponseType)caller.Call("GetTransactionDetails", concreteRequest);
            }
            catch
            {
                return GetTransactionDetailsInfo(transactionId);
            }
        }

        public TransactionSearchResponseType GetTransactionSearchResponseType(DateTime startDate, DateTime endDate)
        {
            CallerServices caller = new CallerServices();
            caller.APIProfile = profile;

            TransactionSearchRequestType concreteRequest = new TransactionSearchRequestType();
            concreteRequest.DetailLevel = new DetailLevelCodeType[] { DetailLevelCodeType.ReturnAll };

            concreteRequest.StartDate = startDate;
            concreteRequest.EndDate = endDate;
            concreteRequest.EndDateSpecified = true;
            concreteRequest.Status = PaymentTransactionStatusCodeType.Success;
            
            TransactionSearchResponseType pp_response = new TransactionSearchResponseType();
            try
            {
                return (TransactionSearchResponseType)caller.Call("TransactionSearch", concreteRequest);
            }
            catch
            {
                return GetTransactionSearchResponseType(startDate, endDate);
            }
        }
    }
}
